package com.ruoyi.training.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import com.ruoyi.training.dto.ProjectTreeSelect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.training.domain.Projects;
import com.ruoyi.training.service.IProjectsService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;

/**
 * 项目管理Controller
 * 
 * @author hcc
 * @date 2025-09-16
 */
@RestController
@RequestMapping("/projects")
public class ProjectsController extends BaseController
{
    @Autowired
    private IProjectsService projectsService;

    /**
     * 查询项目管理列表
     */
    @RequiresPermissions("training:projects:list")
    @GetMapping("/list")
    public AjaxResult list(Projects projects)
    {
        List<Projects> list = projectsService.selectProjectsList(projects);
        return success(list);
    }

    /**
     * 查询项目管理树结构
     */
    @RequiresPermissions("training:projects:list")
    @GetMapping("/projectTree")
    public AjaxResult projectTree(Projects projects)
    {
        List<ProjectTreeSelect> list = projectsService.selectProjectsTreeList(projects);
        return success(list);
    }

    /**
     * 导出项目管理列表
     */
    @RequiresPermissions("training:projects:export")
    @Log(title = "项目管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Projects projects)
    {
        List<Projects> list = projectsService.selectProjectsList(projects);
        ExcelUtil<Projects> util = new ExcelUtil<Projects>(Projects.class);
        util.exportExcel(response, list, "项目管理数据");
    }

    /**
     * 获取项目管理详细信息
     */
    @RequiresPermissions("training:projects:query")
    @GetMapping(value = "/{projectId}")
    public AjaxResult getInfo(@PathVariable("projectId") String projectId)
    {
        return success(projectsService.selectProjectsByProjectId(projectId));
    }

    /**
     * 新增项目管理
     */
    @RequiresPermissions("training:projects:add")
    @Log(title = "项目管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Projects projects)
    {
        Projects projectsByName = projectsService.selectProjectsByProjectName(projects.getProjectName(),projects.getpId());
        if (projectsByName != null){
            return error("新增项目'" + projects.getProjectName() + "'失败，项目名称已存在");
        }
        return toAjax(projectsService.insertProjects(projects));
    }

    /**
     * 修改项目管理
     */
    @RequiresPermissions("training:projects:edit")
    @Log(title = "项目管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Projects projects)
    {
        String projectId = projects.getProjectId();
        int n = projectsService.selectNormalChildrenById(projectId);
        Projects projectsByName = projectsService.selectProjectsByProjectName(projects.getProjectName(),projects.getpId());
        if (projectsByName != null && !projects.getProjectName().equals(projectsByName.getProjectName()))
        {
            return error("修改项目'" + projects.getProjectName() + "'失败，项目名称已存在");
        }
        else if (projects.getpId().equals(projects.getProjectId()))
        {
            return error("修改项目'" + projects.getProjectName() + "'失败，上级项目不能是自己");
        }
        else if (projectsService.selectNormalChildrenById(projectId) > 0 && "1".equals(projects.getProjectStatus()))
        {
            return error("该项目包含未停用的子项目,不能停用");
        }
        return toAjax(projectsService.updateProjects(projects));
    }

    /**
     * 逻辑删除项目管理
     */
    @RequiresPermissions("training:projects:edit")
    @Log(title = "项目管理", businessType = BusinessType.UPDATE)
    @PutMapping("/update/{projectId}")
    public AjaxResult update(@PathVariable("projectId") String projectId)
    {
        return toAjax(projectsService.updateFlagProjects(projectId));
    }

    /**
     *
     * 删除项目管理
     */
    @RequiresPermissions("training:projects:remove")
    @Log(title = "项目管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{projectIds}")
    public AjaxResult remove(@PathVariable String[] projectIds)
    {
        return toAjax(projectsService.deleteProjectsByProjectIds(projectIds));
    }
}
